﻿using NHibernate;
using NHibernate.Cfg;
using SmartRS.com.vn.smartrs.domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace SmartRS.com.vn.smartrs
{
    public class NHibernateHelper
    {
        
        private static ISessionFactory _sessionFactory;

        private NHibernateHelper() { }

        private static ISessionFactory SessionFactory
        {
            get
            {
                try
                {
                    if (_sessionFactory == null)
                    {
                        var configuration = new Configuration();
                        configuration.Configure();
                        configuration.AddAssembly(typeof(FoodKind).Assembly);
                        _sessionFactory = configuration.BuildSessionFactory();
                    }
                    return _sessionFactory;
                }
                catch(Exception ex)
                {
                    throw new Exception("Connect to db happed error : " + ex.Message);
                }
            }
        }

        public static ISession OpenSession()
        {
            return SessionFactory.OpenSession();
        }
    }
}
